#include "Bsp.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

namespace QHSMTST {

static FILE *l_outFile = nullptr;
static struct termios l_oldt;

void BSP_init(void) {
  struct termios newt;
  tcgetattr(STDIN_FILENO, &l_oldt);  // save the terminal state
  newt = l_oldt;
  newt.c_lflag &= ~ICANON;
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);  // set non-canonical mode
  l_outFile = stdout;                       // use the stdout as the output file
}

void BSP_display(char const *msg) { fprintf(l_outFile, "%s", msg); }

void BSP_terminate(int16_t const result) {
  tcsetattr(STDIN_FILENO, TCSANOW, &l_oldt);
  printf("Bye, Bye!\n");
  exit(result);
}

extern "C" void Q_onAssert(char const *const module, int location) {
  fprintf(stderr, "Assertion failed in %s, line %d", module, location);
  exit(-1);
}
}
